package com.recallgo.manage.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.recallgo.manage.entity.Item;
import com.recallgo.manage.entity.ItemDesc;
import com.recallgo.manage.entity.ItemParamItem;
import com.recallgo.manage.mapper.ItemMapper;
import com.recallgo.manage.service.ItemDescService;
import com.recallgo.manage.service.ItemParamItemService;
import com.recallgo.manage.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author gineshuai
 * @since 2018-09-11
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    private ItemDescService itemDescService;

    @Autowired
    private ItemParamItemService itemParamItemService;

    /**
     * 添加商品信息
     * @param item
     * @param desc
     */
    @Transactional
    public void saveItem(Item item, String desc, String itemParams){
        item.setId(null);
        Date date = new Date();
        item.setCreated(date);
        item.setUpdated(date);
        super.insert(item);

        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);
        itemDescService.insert(itemDesc);

        ItemParamItem itemParamItem = new ItemParamItem();
        itemParamItem.setItemId(item.getId());
        itemParamItem.setParamData(itemParams);
        itemParamItem.setCreated(date);
        itemParamItem.setUpdated(date);
        itemParamItemService.insert(itemParamItem);
    }

    /**
     * 修改商品信息
     * @param item
     * @param desc
     */
    @Transactional
    public void modItem(Item item, String desc, String itemParams){
        Date date = new Date();
        item.setUpdated(date);
        EntityWrapper<Item> wrapper = new EntityWrapper<>();
        wrapper.where("id = {0}", item.getId());
        super.update(item, wrapper);

        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        itemDesc.setUpdated(date);
        EntityWrapper<ItemDesc> itemDescWrapper = new EntityWrapper<>();
        itemDescWrapper.where("item_id = {0}", itemDesc.getItemId());
        itemDescService.update(itemDesc, itemDescWrapper);

        ItemParamItem itemParamItem = new ItemParamItem();
        itemParamItem.setParamData(itemParams);
        itemParamItem.setUpdated(date);
        EntityWrapper<ItemParamItem> itemParamItemEntityWrapper = new EntityWrapper<>();
        itemParamItemEntityWrapper.where("item_id = {0}", item.getId());
        itemParamItemService.update(itemParamItem, itemParamItemEntityWrapper);
    }

}
